import os
from tokenize import endpats

students = []

def save() :
    with open('record.txt','w',encoding='utf-8') as f :
        global students
        for student in students :
            f.write(f"[{student['studentId']}]\t{student['name']}\t{student['age']}\t{student['gender']}\t{student['classId']}\n")
        print(f"存档成功，共存储了{len(students)}条学生信息")

def load() :
    if not os.path.exists('record.txt') :
        return

    with open('record.txt','r',encoding='utf-8') as f :
        global students
        students = []
        for line in f :
            '''去掉一个字符串开头和末尾的空白符'''
            line = line.strip()
            tokens = line.split('\t')
            if len(tokens) != 5 :
                print(f'当前行格式有问题,line={line}')
                return
            student = {
                'studentId' : tokens[0],
                'name' : tokens[1],
                'age' : tokens[2],
                'gender' : tokens[3],
                'classId' : tokens[4]
            }
            students.append(student)
        print(f'读档成功，一共读到了{len(students)}条学生信息')


def welcome() :
    print('1.新增学生')
    print('2.删除学生')
    print('3.查找学生')
    print('4.查看学生')
    print('5.退出系统学生（输入0退出）')
    print('请选择功能：')
    choice = input()
    return choice


def add() :
    print('【开始新增学生】')
    studentId : int = int(input("请输入学生的学号"))
    name : str = input('请输入学生的姓名')
    age : int = int(input('请输入学生的年龄'))
    gender : str = input('请输入学生的性别')
    if gender!='男' and gender != '女' :
        print('性别有误')
        return
    classId : str = input('请输入学生的班级')
    student = {
        "studentId" : studentId,
        "name" : name,
        "age" : age,
        "gender" : gender,
        "classId" : classId
    }
    global students
    students.append(student)
    save()
    print('【新增学生成功】')

def delete() :
    print('【开始删除学生】')
    studentId : int = int(input('请输入你要删除的学生的学号'))
    for student in students :
        if student['studentId'] == studentId :
            print(f"删除{student['name']}的信息")
            students.remove(student)
    save()
    print('【删除学生结束】')

def search() :
    print("【查找学生开始】")
    name : str = input("请输入要查找的学生的名字")
    count = 0
    for student in students :
        if student['name']==name :
            count+=1
            print(f"[{student['studentId']}]\t{student['name']}\t{student['age']}\t{student['gender']}\t{student['classId']}")
    print(f"一共有{count}个匹配的同学")
    print("【查找学生结束】")

def show() :
    print('【开始展示所有的学生】')
    for student in students:
        print(f"[{student['studentId']}]\t{student['name']}\t{student['age']}\t{student['gender']}\t{student['classId']}")
    print(f'展示所有的学生完毕,一共显示了{len(students)}条信息')

def main():
    print('+——————————————————————————————————————————+')
    print('|             欢迎来到学生管理系统             |')
    print('|             欢迎来到学生管理系统             |')
    print('|             欢迎来到学生管理系统             |')
    print('+——————————————————————————————————————————+')

    load()
    while True :
        choice = welcome()
        if choice == '1' :
            add()
        elif choice == '2' :
            delete()
        elif choice == '3' :
            search()
        elif choice == '4' :
            show()
        elif choice == '0' :
            print("goodbye")
            break
        else :
            print("输入错误,请重新输入")

main()